import model.user as user
import model.student as student
import model.util_conn as utl
from model import StudentDao


class userDao(object):

    def save(self,user):
         try:
            conn = utl.getConn()
            cursor = conn.cursor()
            sql = "insert into user values({},'{}','{}')".format(user.user_id,user.name,user.passwd)
            cursor.execute(sql)
            conn.commit()
         except:
             print("发生异常")
         finally:
              cursor.close()
              conn.close()
              print("保存成功")


    def delete(self,user):
        try:
            conn = utl.getConn()
            cursor = conn.cursor()
            sql = "delete from user where id={} and uname='{}'".format(user.user_id,user.name)
            cursor.execute(sql)
            conn.commit()
            print("删除成功")
        except:
            print("发生异常")
        finally:
            cursor.close()
            conn.close()


    def findByNameAndPasswd(self,uname,passwd):
        try:
            conn = utl.getConn()
            cursor = conn.cursor()
            sql = "select * from user where uname='{}' and passwd='{}'".format(uname,passwd)
            cursor.execute(sql)
            user = cursor.fetchone()
            return user
        except:
            print("发生异常")

        finally:
            cursor.close()
            conn.close()

    def findAllUser(self):
        try:
            conn = utl.getConn()
            cursor = conn.cursor()
            sql = "select * from user "
            cursor.execute(sql)
            data=cursor.fetchall()
            return data
        except:
            print("发生异常")

        finally:
            conn.close()
            cursor.close()

    def update(self,user):
        try:
            conn = utl.getConn()
            cursor = conn.cursor()
            sql = "update  user set id={} ,uname='{}',passwd ='{}'where id={}".format(user.user_id,user.name,user.passwd,user.user_id)
            cursor.execute(sql)
            data=cursor.fetchall()
            conn.commit()
        except:
            print("发生异常")

        finally:
            cursor.close()
            conn.close()

